package www.yingdi365.com.viewholder;

import android.support.annotation.LayoutRes;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;

import www.yingdi365.com.R;
import www.yingdi365.com.activity.YeMainTZActivity;
import www.yingdi365.com.constant.Constant;
import www.yingdi365.com.model.QingJianBean;
import www.yingdi365.com.photoview.PhotoView;
import www.yingdi365.com.util.DpUtils;
import www.yingdi365.com.util.LogUtil;

/**
 * Created by Administrator on 2017/3/28 0028.
 */
public class YeMainTZViewHolder extends BaseViewHolder<QingJianBean> {


    private final AbsoluteLayout viewAbsolute;
    private final ImageView imageBg;
    private final View viewFuGai;
    private final ImageView imageShanChu;
    private TextView[] textViews;
    private PhotoView[] imageViews;

    public YeMainTZViewHolder(ViewGroup parent, @LayoutRes int res, int viewType) {
        super(parent, res);
        viewAbsolute = $(R.id.viewAbsolute);
        viewFuGai = $(R.id.viewFuGai);
        imageBg = $(R.id.imageBg);
        ImageView imageJiaoBiao = $(R.id.imageJiaoBiao);
        imageShanChu = $(R.id.imageShanChu);
        if (viewType == 1) {
            imageJiaoBiao.setVisibility(View.VISIBLE);
            imageJiaoBiao.setImageResource(R.mipmap.fengmain);
            imageShanChu.setVisibility(View.GONE);
        } else if (viewType == 2) {
            imageJiaoBiao.setVisibility(View.VISIBLE);
            imageJiaoBiao.setImageResource(R.mipmap.fengdi);
            imageShanChu.setVisibility(View.GONE);
        } else {
            imageJiaoBiao.setVisibility(View.GONE);
            imageShanChu.setVisibility(View.VISIBLE);
        }
        viewFuGai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    public void setData(final QingJianBean data) {
        super.setData(data);
        for (int i = 0; i < data.getItems().size(); i++) {
            QingJianBean.ItemsBeanX itemsBeanX = data.getItems().get(i);
            itemsBeanX.setIndex(i);
        }
        textViews = new TextView[data.getItems().size()];
        imageViews = new PhotoView[data.getItems().size()];
        viewAbsolute.post(new Runnable() {
            @Override
            public void run() {
                int width = viewAbsolute.getWidth();
                int height = (int) (1.4375f * width);
                ViewGroup.LayoutParams layoutParams = viewAbsolute.getLayoutParams();
                layoutParams.width = width;
                layoutParams.height = height;
                viewAbsolute.setLayoutParams(layoutParams);
                ViewGroup.LayoutParams layoutParams1 = viewFuGai.getLayoutParams();
                layoutParams1.width = width;
                layoutParams1.height = height;
                viewFuGai.setLayoutParams(layoutParams1);
                if (data.getItems().size() == 0 && TextUtils.isEmpty(data.getBgImage())) {
                    Glide.with(getContext())
                            .load(data.getPreview())
                            .into(imageBg);
                } else {
                    final float baiFenBiWidth = (float) width / 640f;
                    final float baiFenBiHeigh = (float) height / 920f;
                    for (int i = 0; i < data.getItems().size(); i++) {
                        final QingJianBean.ItemsBeanX itemsBean = data.getItems().get(i);
                        switch (itemsBean.getType()) {
                            case "picture":
                                final QingJianBean.ItemsBeanX.PictureBeanX pictureBean = itemsBean.getPicture();
                                float vx = (float) (pictureBean.getVx()) * baiFenBiWidth;
                                float vy = (float) (pictureBean.getVy()) * baiFenBiHeigh;
                                float vWidth = (float) (pictureBean.getVwidth()) * baiFenBiWidth;
                                float vHeight = (float) (pictureBean.getVheight()) * baiFenBiHeigh;
                                imageViews[itemsBean.getIndex()] = new PhotoView(getContext());
                                imageViews[itemsBean.getIndex()].setLayoutParams(new AbsoluteLayout.LayoutParams((int) vWidth, (int) vHeight, (int) vx, (int) vy));
                                imageViews[itemsBean.getIndex()].setScaleType(ImageView.ScaleType.CENTER_CROP);
                                Glide.with(getContext())
                                        .load(pictureBean.getUrl()+ Constant.TuPianYaSuo).listener(new RequestListener<String, GlideDrawable>() {
                                    @Override
                                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                                        return false;
                                    }

                                    @Override
                                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                                        if (itemsBean.isDiy()) {
                                            float scaleW = (float) (pictureBean.getWidth()) / (float) (pictureBean.getVwidth());
                                            float scaleH = (float) (pictureBean.getHeight()) / (float) (pictureBean.getVheight());
                                            float scale = scaleH > scaleW ? scaleW : scaleH;
                                            imageViews[itemsBean.getIndex()].zoomToQuick(scale, (float) (pictureBean.getX() - pictureBean.getVx()) * baiFenBiWidth, (float) (pictureBean.getY() - pictureBean.getVy()) * baiFenBiHeigh);
                                        }
                                        return false;
                                    }
                                })
                                        .into(new SimpleTarget<GlideDrawable>() {
                                            @Override
                                            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                                                imageViews[itemsBean.getIndex()].setImageDrawable(resource);
                                            }
                                        });
                                viewAbsolute.addView(imageViews[itemsBean.getIndex()]);
                                break;
                            case "text":
                                QingJianBean.ItemsBeanX.TextBeanX textBeanX = itemsBean.getText();
                                float vx1 = (float) (textBeanX.getX()) * baiFenBiWidth;
                                float vy1 = (float) (textBeanX.getY()) * baiFenBiHeigh;
                                float vWidth1 = (float) (textBeanX.getWidth()) * baiFenBiWidth;
                                float vHeight1 = (float) (textBeanX.getHeight()) * baiFenBiHeigh * 1.2f;
                                textViews[itemsBean.getIndex()] = new TextView(getContext());
                                textViews[itemsBean.getIndex()].setLayoutParams(new AbsoluteLayout.LayoutParams((int) vWidth1, (int) vHeight1, (int) vx1, (int) vy1 - (int) (0.3f * vHeight1)));
                                textViews[itemsBean.getIndex()].setTextSize(DpUtils.convertPixelsToDp(Float.parseFloat(textBeanX.getFontSize()) * baiFenBiWidth, getContext()));
                                textViews[itemsBean.getIndex()].setGravity(Gravity.CENTER);
                                textViews[itemsBean.getIndex()].setText(textBeanX.getContent());
                                viewAbsolute.addView(textViews[itemsBean.getIndex()]);
                                break;
                        }
                    }
                    Glide.with(getContext())
                            .load(data.getBgImage())
                            .into(imageBg);
                }
            }
        });
        imageShanChu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.LogShitou("YeMainTZViewHolder--list长度",((YeMainTZActivity)getContext()).qingJianBeanList.size()+"" );
                LogUtil.LogShitou("YeMainTZViewHolder--删除的位置", getDataPosition()+"");
                ((YeMainTZActivity)getContext()).qingJianBeanList.remove(getDataPosition());
                ((YeMainTZActivity)getContext()).adapter.remove(getDataPosition());
            }
        });
    }

}
